<template>
    <div>
        <div style="display: flex;flex-direction: row;justify-content: center">
            <el-input v-model="inputInfo" style="width: 200px;margin-right: 10px" type="text" clearable></el-input>
            <el-button icon="el-icon-search" size="medium" style="margin-left: 10px"
                       type="success" @click="handleLocationSearch">搜索
            </el-button>
            <el-button icon="el-icon-plus" size="medium"
                       style="margin-left: 100px" type="primary"
                       @click="handleLocationAddition">新增
            </el-button>
        </div>
        <div style="display: flex;align-items: center;flex-direction: column;margin-left: -25px">
            <el-table :key="centerDialogVisible"
                      :data="locations"
                      stripe
                      style="width: 90%">
                <el-table-column label="ID" prop="id" width="50px"></el-table-column>
                <el-table-column label="物资存放名称" prop="name"></el-table-column>
                <el-table-column label="物资内容" prop="contentString"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            @click="handleEdit(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                :page-size="2"
                :total="paginationTotal"
                background
                layout="prev, pager, next"
                style="margin-top: 5px">
            </el-pagination>
        </div>
        <el-dialog
            :title="title"
            :visible.sync="centerDialogVisible"
            width="60%">
            <el-form :model="selectedLocation" class="selectedLocationForm">
                <el-form-item label="物资存放名称:" prop="name">
                    <el-input v-model="selectedLocation.name" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="物资内容:" prop="contentArray">
                    <el-transfer v-model="selectedLocation.contentArray" :data="supplies"></el-transfer>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                            <el-button @click="centerDialogVisible = false;">取 消</el-button>
                            <el-button type="primary" @click="handleDialogEnter">确 定</el-button>
                        </span>
        </el-dialog>
    </div>
</template>

<script>
import variables from "@/assets/variables";

export default {
    name: "LocationMaintenance",
    data() {
        return {
            locations: [],
            supplies: [],
            title: "",
            inputInfo: "",
            centerDialogVisible: false,
            selectedLocation: {},
            selectedLocationIndex: -1,
        }
    },
    methods: {
        handleLocationAddition() {
            this.title = "新增";
            this.centerDialogVisible = true;
            this.selectedLocation = {
                id: this.locations.length + 1,
                contentArray: [],
                content: [],
            };
        },
        handleLocationSearch() {
            if (this.inputInfo==="") {
                this.locations = variables.locations;
            }else {
                let that = this;
                this.locations = variables.locations.filter(function(location) {
                    if (location.name.includes(that.inputInfo)) return true;
                    for (let supply in location.content) {
                        if (supply instanceof String && supply.includes(that.inputInfo)) return true;
                    }
                    return false;
                });
            }
        },
        handleEdit(index, obj) {
            this.title = "修改";
            this.centerDialogVisible = true;
            this.selectedLocationIndex = index;
            this.selectedLocation = JSON.parse(JSON.stringify(obj));
        },
        handleDelete(index) {
            this.locations.splice(index, 1);
            this.$message({
                type: "success",
                message: "删除成功",
            })
        },
        handleDialogEnter() {
            if (this.title === "新增") {
                let selectedArray = this.selectedLocation.contentArray;
                for (let i in selectedArray) {
                    for (let j in this.supplies) {
                        if (this.supplies[j].key===selectedArray[i]) {
                            this.selectedLocation.content.push(this.supplies[j].label);
                            break;
                        }
                    }
                }
                this.locations.push(this.selectedLocation);
            } else {
                let arr = this.selectedLocation.contentArray;
                this.selectedLocation.content = [];
                for (let i in arr) {
                    for (let j in variables.supplies) {
                        if (variables.supplies[j].id===arr[i]) {
                            this.selectedLocation.content.push(variables.supplies[j].name);
                            break;
                        }
                    }
                }
                this.selectedLocation.contentString = this.selectedLocation.content.join(" , ");
                this.locations[this.selectedLocationIndex] = this.selectedLocation;
            }
            this.centerDialogVisible = false;
        }

    },
    computed: {
        paginationTotal() {
            return this.locations.length;
        }
    },
    mounted() {
        this.locations = variables.locations;
        let arr = variables.supplies;
        this.locations.forEach(function (location) {
            location.contentArray = [];
            for (let i in location.content) {
                for (let j in arr) {
                    if (arr[j].name===location.content[i]) {
                        location.contentArray.push(arr[j].id)
                        break;
                    }
                }
            }
        })
        for (let index in arr) {
            this.supplies.push({
                key: arr[index].id,
                label: arr[index].name
            })
        }
    },
    watch: {
        locations: {
            handler() {
                this.locations.forEach(lo => {
                    lo.contentString = lo.content.join(" , ");
                })
            },
            immediate: true,
        }
    }
}
</script>

<style scoped>
.selectedLocationForm {
    height: 300px;
}

.selectedLocationForm >>> .el-form-item {
    display: inline-block;
    width: 260px;
    float: left;
    margin: 5px 15px;
}
.el-transfer {
    display: flex;
    width: 550px;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
}

.el-transfer >>> button {
    margin: 20px -10px
}

</style>